<?php

namespace app\admin\controller;

use think\App;
use think\facade\Db;
use bxkj_module\controller\Auth;
use think\facade\View;

class Controller extends Admin
{
    protected $allows = [
        "login" => ["index"],
    ];

    protected static $is_not_rule = [
        'admin/login/logoout', 'admin/index/index', 'admin/menu/index'
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        if (!is_allow($this->allows) && empty($this->admin)) {
            $this->redirectTo(url('/admin/login/index'));
        }

        if (!empty($this->admin)) {
            $no_check_user_list = Db::name($this->sysName)->where('is_admin', 1)->column('admin_id');
            $auth = new Auth();
            $controller = strtolower(request()->controller());
            $action = strtolower(request()->action());
            $url = $this->sysName . '/' . $controller . "/" . $action;
            if (!in_array(session($this->sysName)['admin_id'], $no_check_user_list)) {
                if (!in_array($url, self::$is_not_rule)) {
                    if (!$auth->check($url, session($this->sysName)['admin_id'])) {
                        $this->error(lang('NO_PERMISSION'));
                    }
                }
            }
            View::assign('admin', $this->admin);
        }
    }
}